<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <el-form
      ref="ruleForm"
      label-width="100px"
      :model="ruleForm"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="填写部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="ruleForm.code" placeholder="设置部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="ruleForm.managerId"
          placeholder="部门负责人"
          style="width: 100%"
        >
          <el-option
            v-for="item in departmentHead"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="ruleForm.introduce"
          type="textarea"
          placeholder="1-200字以内"
          :rows="4"
        />
      </el-form-item>
      <el-form-item center>
        <el-row type="flex" justify="center">
          <el-col :span="16">
            <el-button type="primary" @click="btnOK">确认</el-button>
            <el-button @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  editorialDepartment,
  getDepartment,
  getDepartmentHead,
  addNewDepartments,
  getDepartmentDetails
} from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // eslint-disable-next-line vue/prop-name-casing
    CurrentID: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    const validateName = async(rule, value, callback) => {
      const res = await getDepartment()
      console.log(res)
      if (
        res.data.data.some(
          (item) => item.name === value && this.CurrentID !== item.id
        )
      ) {
        callback(new Error('已有该部门'))
      } else {
        callback()
      }
    }
    const validateCode = async(rule, value, callback) => {
      const res = await getDepartment()
      if (
        res.data.data.some(
          (item) => item.code === value && this.CurrentID !== item.id
        )
      ) {
        callback(new Error('已有该编码'))
      } else {
        callback()
      }
    }
    return {
      departmentHead: [], // 接收获取的部门负责人列表
      ruleForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '', // 部门介绍
        pid: '' // 部门父级ID
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '长度在 1 到 100 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getDepartmentHead()
  },
  methods: {
    close() {
      this.$refs.ruleForm.resetFields() // 重置表单
      this.$emit('update:showDialog', false)
    },
    async getDepartmentHead() {
      // 获取部门负责人列表
      const res = await getDepartmentHead()
      this.departmentHead = res.data.data
    },
    btnOK() {
      this.$refs.ruleForm.validate(async(isOk) => {
        if (isOk) {
          if (this.title === '添加子部门') {
            await addNewDepartments({ ...this.ruleForm, pid: this.CurrentID }) // 新增部门请求
            this.$emit('updateDepartment') // 请求成功后通知父组件更新数据
          } else if (this.title === '编辑部门') {
            await editorialDepartment(this.CurrentID, this.ruleForm)
            this.$emit('updateDepartment') // 请求成功后通知父组件更新数据
          }
          this.$message.success('编辑数据成功')
          this.close() // 清除表单，关闭弹出页面
        }
      })
    },
    async getDepartmentDetail() {
      // 获取部门详情请求
      const res = await getDepartmentDetails(this.CurrentID)
      this.ruleForm = res.data.data
    }
  }
}
</script>
